package com.infoauto.opc.gateway.messages.factory;

import com.infoauto.opc.gateway.messages.handler.MqttListener;
import com.infoauto.opc.gateway.messages.model.MqttConsumer;
import com.infoauto.opc.gateway.messages.model.MqttSender;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;

import java.util.UUID;


public class MqttFactory {

    private static MqttPahoClientFactory clientFactory;

    public MqttFactory(String serverUris, String username, String password) {
        MqttFactory.clientFactory = clientFactory(serverUris, username, password);
    }

    public static MqttSender createSender() throws MqttException {
        return createSender(clientFactory, newClientId(), null);
    }

    public static MqttSender createSender(MqttListener mqttListener) throws MqttException {
        return createSender(clientFactory, newClientId(), mqttListener);
    }

    public static MqttSender createSender(String clientId, MqttListener mqttListener) throws MqttException {
        return createSender(clientFactory, clientId, mqttListener);
    }

    public static MqttSender createSender(String serverUris, String username, String password, String clientId,
            MqttListener mqttListener) throws MqttException {
        return createSender(clientFactory(serverUris, username, password), clientId, mqttListener);
    }

    private static MqttSender createSender(MqttPahoClientFactory clientFactory, String clientId,
            MqttListener mqttListener) throws MqttException {
        MqttSender mqttSender = new MqttSender(clientFactory, clientId, mqttListener);
        mqttSender.connect();
        return mqttSender;
    }

    public static MqttConsumer createConsumer(MqttListener mqttListener) throws MqttException {
        return createConsumer(clientFactory, newClientId(), mqttListener);
    }

    public static MqttConsumer createConsumer(String clientId, MqttListener mqttListener) throws MqttException {
        return createConsumer(clientFactory, clientId, mqttListener);
    }

    public static MqttConsumer createConsumer(String serverUris, String username, String password, String clientId,
            MqttListener mqttListener) throws MqttException {
        return createConsumer(clientFactory(serverUris, username, password), clientId, mqttListener);
    }

    private static MqttConsumer createConsumer(MqttPahoClientFactory clientFactory, String clientId,
            MqttListener mqttListener) throws MqttException {
        MqttConsumer mqttConsumer = new MqttConsumer(clientFactory, clientId, mqttListener);
        mqttConsumer.connect();
        return mqttConsumer;
    }

    private static MqttPahoClientFactory clientFactory(String serverUris, String username, String password) {
        DefaultMqttPahoClientFactory clientFactory = new DefaultMqttPahoClientFactory();
        clientFactory.setServerURIs(StringUtils.split(serverUris, ","));
        clientFactory.setUserName(username);
        clientFactory.setPassword(password);
        //设置连接超时时间（默认30秒）
        clientFactory.setConnectionTimeout(30);
        //设置会话心跳时间（默认60秒）
        clientFactory.setKeepAliveInterval(60);
        //设置重新连接时记住状态（不清除）
        clientFactory.setCleanSession(false);
        return clientFactory;
    }

    private static String newClientId() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

}
